  <!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>模块增加页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="/res/layui/css/layui.css" media="all">
    <script src="/res/js/jquery.js"></script>

</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>模块信息维护</legend>
</fieldset>
<form class="layui-form" action="" lay-filter="example" id="myform">
    <input type="hidden" id="tree1">
    <input type="hidden" name="modelId" id="modelId"><!-- 加一个隐藏主键 -->
    <div class="layui-form-item">
        <label class="layui-form-label">上级模块</label>
        <div class="layui-input-inline">
            <input type="text" id="tree" name="tree" lay-filter="tree" class="layui-input" placeholder="请选择上级模块"/>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">模块名称</label>
            <div class="layui-input-inline">
                <input type="text" name="modelName" id="modelName" lay-verify="required"  placeholder="请输入模块名称" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">模块备注</label>
        <div class="layui-input-inline">
            <input type="text" name="modelRemark" id="modelRemark" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">url地址</label>
        <div class="layui-input-inline">
            <input type="text" name="modelUrl" id="modelUrl" autocomplete="off" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="button" class="layui-btn layui-btn-primary" id="close_btn">关闭</button>
        </div>
    </div>
</form>

<script src="/res/layui/layui.js"></script>


<script>


    layui.use(['form', 'layedit', 'laydate', 'jquery','treeSelect', 'tree'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , $ = layui.$
            ,treeSelect = layui.treeSelect;

        $.ajaxSetup({
            async: false
        });

        /**
         * 当点击编辑按钮时会传入一个id到form。html页面
         */
        let tree;
        let id = getQueryString("id");
        if (id) {
            let url = "/logisticsModel/show";
            let data_ = {id: id};
            $.post(url, data_, function (resp) {
                if (resp.state == 1) {
                    form.val('example', resp.data);

                    tree=resp.data.modelParent;

                    //重新渲染
                    form.render();
                }
            }, "json");
        }

        treeSelect.render({
            // 选择器
            elem: '#tree',
            id:'mTree',
            // 数据
            data: '/logisticsModel/selectShow',
            // 异步加载方式：get/post，默认get
            type: 'get',
            // 占位符
            placeholder: '不选默认为根目录',
            // 是否开启搜索功能：true/false，默认false
            search: true,
            // 点击回调
            click: function (d) {
                console.log(d.current.id);
                $("#tree1").val(d.current.id);
            },
            // 加载完成后的回调函数
            success: function (d) {
                console.log(d);
//                选中节点，根据id筛选
                if(tree){
                    treeSelect.checkNode('mTree', tree);
                }else{

//                刷新树结构
                    treeSelect.refresh("tree");
                }
            }



        });


        //监听提交
        form.on('submit(demo1)', function (data) {
            let formData = new FormData(document.getElementById("myform"));
            console.info($("#tree1").val());
            formData.append("tree1",$("#tree1").val());
            let data_ ={tree1:$("#tree1").val(),modelId:$("#modelId").val(),modelName:$("#modelName").val(),modelRemark:$("#modelRemark").val(),modelUrl:$("#modelUrl").val()}
            let url = "/logisticsModel/addAndUpdate";
            $.post(url, data_, function (resp) {
                if (resp.state == 1) {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.table.reload('dataLXList');
                } else {
                    layer.msg(resp.mgs);
                }

            }, "json");
            return false;
        });

        /**
         * 得到地址上得值
         * @param id
         * @returns {string|null}
         */
        function getQueryString(id) {
            var reg = new RegExp("(^|&)" + id + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
            console.info(r);
        }

        $(function () {
            $("#close_btn").on("click",function () {
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            });
        });


    });
</script>
</body>
</html>